package com.aaa.cars.service.impl;

import com.aaa.cars.dao.MaintainShopDao;
import com.aaa.cars.entity.MaintainShop;
import com.aaa.cars.service.MaintainShopService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 汽车维修表(MaintainShop)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 19:28:34
 */
@Service("maintainShopService")
@Transactional
public class MaintainShopServiceImpl implements MaintainShopService {
    @Resource
    private MaintainShopDao maintainShopDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MaintainShop queryById(Integer id) {
        return this.maintainShopDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param maintainShop 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, MaintainShop maintainShop) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<MaintainShop> pageInfo = new PageInfo<>(maintainShopDao.queryAll(maintainShop));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MaintainShop> queryAllByLimit(int offset, int limit) {
        return this.maintainShopDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param maintainShop 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainShop insert(MaintainShop maintainShop) {
        this.maintainShopDao.insert(maintainShop);
        return maintainShop;
    }

    /**
     * 修改数据
     *
     * @param maintainShop 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainShop update(MaintainShop maintainShop) {
        this.maintainShopDao.update(maintainShop);
        return this.queryById(maintainShop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.maintainShopDao.deleteById(id) > 0;
    }

    /**
     * 通过实体作为筛选条件查询
     * @param maintainShop 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAlls(MaintainShop maintainShop) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), maintainShopDao.queryAll(maintainShop));
    }
}